.TH std::stack::stack 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::stack::stack \- std::stack::stack

.SH Synopsis
   stack() : stack(Container()) {}                     \fB(1)\fP \fI(since C++11)\fP
   explicit stack( const Container& cont = Container()                   \fI(until C++11)\fP
   );
   explicit stack( const Container& cont );                              \fI(since C++11)\fP
   explicit stack( Container&& cont );                     \fB(3)\fP           \fI(since C++11)\fP
   stack( const stack& other );                            \fB(4)\fP           (implicitly
                                                                         declared)
                                                                         \fI(since C++11)\fP
   stack( stack&& other );                                 \fB(5)\fP           (implicitly
                                                                         declared)
   template< class InputIt >                               \fB(6)\fP           (since C++23)
   stack( InputIt first, InputIt last );
   template< class Alloc >                                 \fB(7)\fP           \fI(since C++11)\fP
   explicit stack( const Alloc& alloc );
   template< class Alloc >                                 \fB(8)\fP           \fI(since C++11)\fP
   stack( const Container& cont, const Alloc& alloc ); \fB(2)\fP
   template< class Alloc >                                 \fB(9)\fP           \fI(since C++11)\fP
   stack( Container&& cont, const Alloc& alloc );
   template< class Alloc >                                 \fB(10)\fP          \fI(since C++11)\fP
   stack( const stack& other, const Alloc& alloc );
   template< class Alloc >                                 \fB(11)\fP          \fI(since C++11)\fP
   stack( stack&& other, const Alloc& alloc );
   template< class InputIt, class Alloc >
   stack( InputIt first, InputIt last, const Alloc&        \fB(12)\fP          (since C++23)
   alloc );
   template< container-compatible-range<T> R>              \fB(13)\fP          (since C++23)
   stack( std::from_range_t, R&& rg );
   template< container-compatible-range<T> R, class
   Alloc >                                                 \fB(14)\fP          (since C++23)
   stack( std::from_range_t, R&& rg, const Alloc&
   alloc );

   Constructs new underlying container of the container adaptor from a variety of data
   sources.

   1) Default constructor. Value-initializes the container.
   2) Copy-constructs the underlying container c with the contents of cont.
   This is also the default constructor.
   \fI(until C++11)\fP
   3) Move-constructs the underlying container c with std::move(cont).
   4) Copy constructor. The adaptor is copy-constructed with the contents of other.c.
   5) Move constructor. The adaptor is constructed with std::move(other.c).
   6) Constructs the underlying container c with the contents of the range
   [first, last). This overload participates in overload resolution only if InputIt
   satisfies LegacyInputIterator.
   7-12) These constructors participate in overload resolution only if
   std::uses_allocator<Container, Alloc>::value is true, that is, if the underlying
   container is an allocator-aware container (true for all standard library containers
   that can be used with stack).
   7) Constructs the underlying container using alloc as allocator, as if by c(alloc).
   8) Constructs the underlying container with the contents of cont and using alloc as
   allocator, as if by c(cont, alloc).
   9) Constructs the underlying container with the contents of cont using move
   semantics while utilizing alloc as allocator, as if by c(std::move(cont), alloc).
   10) Constructs the adaptor with the contents of other.c and using alloc as
   allocator, as if by c(other.c, alloc).
   11) Constructs the adaptor with the contents of other using move semantics while
   utilizing alloc as allocator, as if by c(std::move(other.c), alloc).
   12) Constructs the underlying container with the contents of the range [first, last)
   using alloc as allocator, as if by c(first, last, alloc). This overload participates
   in overload resolution only if InputIt satisfies LegacyInputIterator.
   13) Constructs the underlying container with
   ranges::to<Container>(std::forward<R>(rg)).
   14) Constructs the underlying container with
   ranges::to<Container>(std::forward<R>(rg), alloc).

.SH Parameters

   alloc                -          allocator to use for all memory allocations of the
                                   underlying container
   other                -          another container adaptor to be used as source to
                                   initialize the underlying container
   cont                 -          container to be used as source to initialize the
                                   underlying container
   first, last          -          range of elements [first, last) to initialize with
   rg                   -          a container compatible range, that is, an
                                   input_range whose elements are convertible to T
.SH Type requirements
   -
   Alloc must meet the requirements of Allocator.
   -
   Container must meet the requirements of Container. The constructors taking an
   allocator parameter participate in overload resolution only if Container meets the
   requirements of AllocatorAwareContainer.
   -
   InputIt must meet the requirements of LegacyInputIterator.

.SH Complexity

   Same as the corresponding operation on the wrapped container.

.SH Notes

               Feature-test macro               Value    Std           Feature
                                                               Iterator pair
                                                               constructors for
   __cpp_lib_adaptor_iterator_pair_constructor 202106L (C++23) std::queue and
                                                               std::stack; overloads
                                                               \fB(6)\fP and \fB(12)\fP
                                                               Ranges-aware
   __cpp_lib_containers_ranges                 202202L (C++23) construction and
                                                               insertion; overloads
                                                               \fB(13)\fP and \fB(14)\fP

.SH Example


// Run this code

 #include <cassert>
 #include <deque>
 #include <iostream>
 #include <memory>
 #include <ranges>
 #include <stack>

 int main()
 {
     std::stack<int> c1;
     c1.push(5);
     assert(c1.size() == 1);

     std::stack<int> c2(c1);
     assert(c2.size() == 1);

     std::deque<int> deq{3, 1, 4, 1, 5};
     std::stack<int> c3(deq); // overload (2)
     assert(c3.size() == 5);

 # ifdef __cpp_lib_adaptor_iterator_pair_constructor
     const auto il = {2, 7, 1, 8, 2};
     std::stack<int> c4{il.begin(), il.end()}; // C++23, (6)
     assert(c4.size() == 5);
 # endif

 # if __cpp_lib_containers_ranges >= 202202L
     // C++23, overload (13)
     auto c5 = std::stack(std::from_range_t, std::ranges::iota(0, 42));
     assert(c5.size() == 42);

     // the same effect with pipe syntax, internally uses overload (13)
     auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::stack>();
     assert(c6.size() == 42);

     std::allocator<int> alloc;

     // C++23, overload (14)
     auto c7 = std::stack(std::from_range_t, std::ranges::iota(0, 42), alloc);
     assert(c7.size() == 42);

     // the same effect with pipe syntax, internally uses overload (14)
     auto c8 = std::ranges::iota(0, 42) | std::ranges::to<std::stack>(alloc);
     assert(c8.size() == 42);
 # endif
 }

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to      Behavior as published       Correct behavior
   P0935R0 C++11      default constructor was explicit made implicit

.SH See also

   operator= assigns values to the container adaptor
             \fI(public member function)\fP
